package botoseis.mainGui.database;



import java.awt.Component;
import java.awt.Frame;

import java.util.ArrayList;
import java.util.List;

import org.jzy3d.chart.Chart;
import org.jzy3d.colors.Color;
import org.jzy3d.colors.ColorMapper;
import org.jzy3d.colors.colormaps.ColorMapRainbow;
import org.jzy3d.maths.Coord3d;
import org.jzy3d.plot3d.builder.Builder;
import org.jzy3d.plot3d.primitives.Shape;
import org.jzy3d.plot3d.rendering.canvas.Quality;
import org.jzy3d.plot3d.rendering.legends.colorbars.ColorbarLegend;
import org.jzy3d.plot3d.rendering.view.modes.ViewPositionMode;


import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class PlotSurface {
    Connection con=null;
    ResultSet rs = null;
    PreparedStatement pst = null;
    

  
 public PlotSurface(){        
        List<Coord3d> coords = getSurf();
         
        // Create the object to represent the function over the given range.
        final Shape surface = (Shape) Builder.buildDelaunay(coords);
        surface.setColorMapper(new ColorMapper(new ColorMapRainbow(), surface.getBounds().getZmin(), surface.getBounds().getZmax(), new Color(1, 1, 1, 1f)));
        surface.setFaceDisplayed(true);
        surface.setWireframeDisplayed(false);
        surface.setWireframeColor(Color.BLACK);

        // Create a chart that updates the surface colormapper when scaling changes
        chart = new Chart(Quality.Advanced);
    chart.getScene().getGraph().add(surface);
       chart.getView().setViewPositionMode(ViewPositionMode.TOP);
    
    // TODO: solve bug on chart.getView().setProjection(Camera.PERSPECTIVE);
    
    // Setup a colorbar for the surface object and add it to the scene
    surface.setLegend(new ColorbarLegend(surface, chart.getView().getAxe().getLayout()));

  }

  public Chart getChart() {
    return chart;
  }

  protected Chart chart;
    
        
    public final List<Coord3d> getSurf(){
      List<Coord3d> coords = new ArrayList<Coord3d>();
        double x;
        double y;
        double z;
        
        con=DBConnect.ConnectDb();
             try {  
                   String queryP ="SELECT  X1,Y1,Z1 FROM ATTRXYZ LIMIT 15000 ";
                   pst=con.prepareStatement(queryP);
                   rs = pst.executeQuery();
                   
                while (rs.next()) { 
                 Object ob1 =  rs.getObject(1);
                Object ob2 =  rs.getObject(2);
                Object ob3 = rs.getObject(3);

                String str1 = ob1.toString();
                x = Double.valueOf(str1).doubleValue(); 

                String str2 = ob2.toString();
                y = Double.valueOf(str2).doubleValue(); 
                
                String str3 = ob3.toString();
                z = Double.valueOf(str3).doubleValue(); 

               //JOptionPane.showMessageDialog(null,x+"-"+y+"-"+z);
                  coords.add(new Coord3d(x,y,z));
                } 
                
                // Drop existing table
               // String queryDrop="DROP TABLE IF EXISTS ATTRXYZ";
               // pst=con.prepareStatement(queryDrop);
                //pst.executeUpdate();    
                
            } catch (Exception e) {  
            System.out.println("Retrieving Data Fail");  
            } finally {
                 try {
                 rs.close();
                 pst.close();
                //con.close();
                }
                 catch (Exception e) {
                 }
             } 

        return coords;
    }
    


    public static void main(String[] args){
       PlotSurface myChart2 = new PlotSurface();

        Frame frame = new Frame();
        frame.setSize(1000, 1000);
        frame.add((Component)myChart2.getChart().getCanvas());
       frame.setBackground(java.awt.Color.WHITE);
        frame.setVisible(true);
    }
}